#include<iostream>
#include<list>
using namespace std;
class Map {
public:
	int n;
	list<int>* tmp;
	bool* visited;
	Map(int N) {
		n = N;
		tmp = new list<int>[n];
		visited = new bool[n];
		for (int i = 0; i < n; i++) {
			visited[i] = false;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				char ch;
				cin >> ch;
				if (ch == '1') {
					tmp[i].push_back(j);
				}
			}
		}
	}
	void DFS(int v) {
		visited[v] = true;
		cout << v << ' ';
		for (auto i = tmp[v].begin(); i != tmp[v].end(); i++) {
			if (!visited[*i]) {
				DFS(*i);
			}
		}
	}
	void test() {
		for (int i = 0; i < n; i++) {
			if (!visited[i]) {
				DFS(i);
			}
		}
	}
};
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n;
		cin >> n;
		Map M(n);
		M.DFS(0);
		M.test();
		cout << endl;
	}
	return 0;
}